package com.hjj.springboot.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hjj.springboot.entity.Order;
import com.hjj.springboot.entity.OrderTips;
import com.hjj.springboot.entity.Promble;
import com.hjj.springboot.entity.R;
import com.hjj.springboot.service.IOrderService;
import com.hjj.springboot.service.IOrderTipsService;
import com.hjj.springboot.service.IPrombleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/tips")
public class OrderTipsController {

    @Autowired
    private IOrderTipsService orderTipsService;

    @Autowired
    private IOrderService orderService;

    @RequestMapping("/create")
    public R create(@RequestBody OrderTips orderTips){
        orderTips.setUpTime(new Date());
        System.out.println(orderTips);
        orderTipsService.save(orderTips);
        return R.ok();
    }

    @GetMapping("/list")
    public R list(Integer type){
        List<OrderTips> orderTipsList=null;
        Map<String,Object> resultMap=new HashMap<>();
        if (type == 0){
            orderTipsList = orderTipsService.list(new QueryWrapper<OrderTips>().orderByDesc("id"));
        }else {
            orderTipsList = orderTipsService.list(new QueryWrapper<OrderTips>().eq("type",type).orderByDesc("id"));
        }
        for (OrderTips orderTips : orderTipsList) {
            Order order=orderService.getOne(new QueryWrapper<Order>().eq("orderNo",orderTips.getOrderNo()));
            orderTips.setOrder(order);
        }
        resultMap.put("orderTipsList",orderTipsList);
        return R.ok(resultMap);
    }

    @GetMapping("/delete")
    public R delete(Integer id){
        orderTipsService.removeById(id);
        return R.ok();
    }
}

